package com.uphone.kingmall.activity.personal;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.china.zhuoyue.R;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.activity.personal.set.WalletCashActivity;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.GetVipCenterInfoBean;
import com.uphone.kingmall.bean.YongJinRankingBean;
import com.uphone.kingmall.fragment.YongJinRankingFragment;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.intent.IntentUtils;
import com.uphone.kingmall.view.indicator.IndicatorAdapter;
import com.uphone.kingmall.view.indicator.TrackIndicatorView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 王雨
 *     e-mail :
 *     time   : 2018/07/31
 *     desc   :佣金
 *     version: 1.0
 * </pre>
 */
public class SmallChargeActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_small_charge)
    TextView tvSmallCharge;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_money_today)
    TextView tvMoneyToday;
    @BindView(R.id.tv_ranking_today)
    TextView tvRankingToday;
    @BindView(R.id.tv_tixian)
    TextView tvTixian;
    @BindView(R.id.rl_detail)
    RelativeLayout rlDetail;
    @BindView(R.id.content)
    LinearLayout content;
    private Double wallet = 0.0;
    @BindView(R.id.track_indicator)
    protected TrackIndicatorView trackIndicator;
    @BindView(R.id.viewPager)
    protected ViewPager viewPager;
    private List<Fragment> fragments;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_small_charge;
    }

    @Override
    public void initView() {
        setStatusBar();
    }

    @Override
    public void initData() {
        fragments = new ArrayList<>();
        fragments.add(YongJinRankingFragment.newInstance(1));
        fragments.add(YongJinRankingFragment.newInstance(2));
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            }
        });
        trackIndicator.setAdapter(viewPager, new IndicatorAdapter(this, new IndicatorAdapter.OnTabClickListener() {
            @Override
            public void tabIvClick(boolean selected) {
            }
        }, "日榜单","总榜单"));
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {

        OkGoUtils.normalRequest(MyUrl.getUserLingQianInfo, null, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    try {
                        if (code == 0) {
                            JSONObject jsonObject = new JSONObject(body);
                            JSONObject object = jsonObject.getJSONObject("lingqian");
                            wallet = object.getDouble("lingqian");
                            tvMoney.setText(wallet + "");
                            String total = object.getString("leiji");
                            tvSmallCharge.setText("累计佣金:" + total);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });

        OkGoUtils.normalRequest(MyUrl.toDayEarnings, null, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    try {
                        if (code == 0) {
                            JSONObject jsonObject = new JSONObject(body);
                            String recordResumeVo = jsonObject.getString("recordResumeVo");
                            YongJinRankingBean.MapBean.RecordResumeVoBean bean = GsonUtils.getGson().fromJson(recordResumeVo, YongJinRankingBean.MapBean.RecordResumeVoBean.class);
                            if (bean != null) {
                                tvMoneyToday.setText(bean.getMoney() + "");
                                tvRankingToday.setText(bean.getRunm() == 0 ? "暂无排名" : "第" + bean.getRunm() + "名");
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_tixian, R.id.rl_detail, R.id.ll_ranking})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.tv_tixian:
                OkGoUtils.normalRequestGet(MyUrl.GetVipInfo, new onNormalRequestListener() {
                    @Override
                    public void onSuccess(String json, int code) {
                        if (code == 0) {
                            GetVipCenterInfoBean bean = GsonUtils.getGson().fromJson(json, GetVipCenterInfoBean.class);
                            if (bean.getCode() == 0) {
                                if (bean.getData().getWay() == 1) {
                                    showSharePop();
                                } else {
                                    IntentUtils.getInstance().with(SmallChargeActivity.this, WalletCashActivity.class).putDouble("wallet", wallet).start();
                                }
                            } else {
                                showSharePop();
                            }

                        }
                    }

                    @Override
                    public void onError(Response<String> response) {

                    }
                });

                break;
            case R.id.rl_detail:
                IntentUtils.getInstance().with(this, YongJinListActivity.class).putInt(ConstansUtils.TYPE, YongJinListActivity.TYPE_WALLET).start();
                break;
//            case R.id.ll_ranking:
//                IntentUtils.getInstance().with(this, YongJinRankingActivity.class).start();
//                break;
        }
    }

    private PopupWindow pop;

    public void showSharePop() {
        View view = getLayoutInflater().inflate(R.layout.kingmall_pop_common_tips, null);
        LinearLayout parent = (LinearLayout) view.findViewById(R.id.parent);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        TextView tv_tips = (TextView) view.findViewById(R.id.tv_tips);
        TextView tv_left = (TextView) view.findViewById(R.id.tv_left);
        TextView tv_right = (TextView) view.findViewById(R.id.tv_right);


        parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
        tv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
                openActivity(VipBuyActivity.class);
            }
        });

        pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        pop.setFocusable(true);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.showAtLocation(content, Gravity.CENTER, 0, 0);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

}
